package cn.iocoder.yudao.framework.jackson.core.databind;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.LocalDate;

public class LocalDateDeserializer  extends JsonDeserializer<LocalDate> {
    public static final LocalDateDeserializer INSTANCE = new LocalDateDeserializer();
    @Override
    public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
        DateTime parse = null;
        try {
            parse = DateUtil.parse(p.getText());
        } catch (Exception e) {
            parse= DateTime.of(p.getLongValue());
        }
        return parse.toLocalDateTime().toLocalDate();
    }
}
